/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.vu.demo.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author VUHOLMES
 */
public class DataUtils {
    private static Connection con; 
    private static int count; 
    private String connect_str;
    private String userid;
    private String password;
    
    public void setConnectData(String db_connect_str,String db_userid, String db_password){
        connect_str = db_connect_str;
        userid = db_userid;
        password = db_password;
    }
    
    public String[] getConnectData(){
        String[] data = new String[3];
        data[0] = connect_str;
        data[1] = userid;
        data[2] = password;
        return data;
    }
    
    public static void connectDb(String db_connect_str,String db_userid, String db_password) {
        try{
            // Connect Normal
//            Class.forName("com.mysql.jdbc.Driver").newInstance();
//            con = DriverManager.getConnection(db_connect_str, db_userid, db_password);
        	
        	//connect postgresql
        	Class.forName("org.postgresql.Driver");
        	con = DriverManager.getConnection(db_connect_str, db_userid, db_password);
        }
        catch(ClassNotFoundException cnfe){
            try { 
                throw new Exception("Cannot find the specified driver.", cnfe); 
            } 
            catch (Exception e) { 
            } 
        }
        catch(Exception e){
            System.out.println("Error, class Connect \n Method: connectDb()"); 
            System.out.println(e); 
        }
    }
    
    public static Connection getConnect() { 
        return con; 
    }
    
    public static void disconnectDb() { 
        try { 
            System.out.println("Disconnecting..." + count); 
            con.close(); 
        } 
        catch (SQLException sqle) { 
            System.out.println(sqle.getMessage()); 
        } 
    } 
    
    public static ResultSet Doc(String chuoi_lenh) throws Exception 
    { 
    	Class.forName("org.postgresql.Driver");
        con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/mydb", "postgres", "tv09914032");
        
        Statement stmt = con.createStatement(); 
        ResultSet rs= stmt.executeQuery(chuoi_lenh); 

//	      while (rs.next()) {
//		      String entry = rs.getString("name");
//		      System.out.println(entry);
//		  }
        
        return rs; 
    }
    
//    public static void main(String[] args) throws Exception {
//		
//        PreparedStatement pstmt = null;
//        try {
//        	 Class.forName("org.postgresql.Driver");
//             con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/mydb", "postgres", "tv09914032");
//             /*String id = "ID";
//            String name="Name";
//            String sex = "Sex";
//            String birthday = "Birthday";
//            String city = "City";
//            String email = "Email";
//            String history = "History";
//            char cm='"';
//            String query1 = "INSERT INTO struts_test.tblemployee " +
//            		"("+cm+name+cm+", "+cm+sex+cm+", "+cm+birthday+cm+", "+cm+city+cm+", "+cm+email+cm+", "+cm+history+cm+") VALUES (?, ?, ?, ?, ?, ?)";*/
//            String query2 = "INSERT INTO struts_test.tblemployee (Id, name, sex, birthday, city, email, history)VALUES(?, ?, ?, ?, ?, ?, ?)";
//            pstmt = con.prepareStatement(query2);
//            pstmt.setInt(1,11);
//            pstmt.setString(2, "unknown"); 
//            pstmt.setString(3, "unknown"); 
//            pstmt.setString(4, "unknown"); 
//            pstmt.setString(5, "unknown"); 
//            pstmt.setString(6, "unknown"); 
//            pstmt.setString(7, "unknown"); 
//            System.out.println(pstmt);
//            int result = pstmt.executeUpdate();
//            if(result > 0){
//                System.out.println("Employee insert successfully"); 
//                
//            }
//            else{
//            	System.out.println("Employee insert failed");
//            }
//            
//            String query = "SELECT * FROM struts_test.tblemployee";
//            
//            Class.forName("org.postgresql.Driver");
//            con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/mydb", "postgres", "tv09914032");
//            
//            Statement stmt = con.createStatement(); 
//            ResultSet rs= stmt.executeQuery(query); 
//            while (rs.next()) {
//            	String entry1 = rs.getString("id");
//                String entry2 = rs.getString("name");
//                System.out.print(entry1+" "+entry2);
//                System.out.println();
//            }
//        }
//        catch(Exception e){
//            e.printStackTrace();
//        }
//    }
}
